﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sio.Mdm.Types.Database;
using Sio.Mdm.Client;
using Sio.Mdm.Server;
using Sio.Mdm.Entities;
using Sio.Mdm.BF.Ldap;
using Sio.Mdm.Types;

namespace Sio.Mdm.BL.Ldap
{
    class LdapLogHelper
    {
        #region Declarations
        Session _session = null;
        private int _ldapPraviloID;
        private int _ldapStreznikID;
        private int _ldapTipSinhronizacije;
        private int _INS = 0;
        private int _DEL = 0;
        private int _UPD = 0;
        private int _GINS = 0;      // Group INS
        private int _GDEL = 0;      // Group DEL
        private int _status = 0;        // 0 - OK, 1 - WARNING, 2 - ERROR
        private eSyncType _syncType;
        private DateTime _startTime;

        private string _Report="";
        private string _Error="";
        private string _Warning="";
        private bool _sessionStarted = false;

        public enum eSyncType
        {
            Automatic = 0,
            UserRequestPravilo = 1,
            UserRequestOrgarnizacija = 2,
            UserRequestData = 3
        }
        #endregion

        #region Public
        public LdapLogHelper(eSyncType type, Session session)
        {
            _syncType = type;
            _session = session;
        }

        public void SetPravilo(int praviloId, int streznikId)
        {
            if (_sessionStarted) SaveLog();

            Init();
            _sessionStarted = true;
            _ldapPraviloID = praviloId;
            _ldapStreznikID = streznikId;
        }

        public void AddReport(string report)
        {
            _Report = String.Format("{0} | {1}", _Report, report);
        }

        public void AddWarning(string warning)
        {
            _Warning = String.Format("{0} | {1}", _Warning, warning);
        }

        public void AddError(string error)
        {
            _Error = String.Format("{0} | {1}", _Error, error);
        }

        #region Add INS DEL UPD
        public void AddINS() {
            _INS++;
        }

        public void AddUPD()
        {
            _UPD++;
        }

        public void AddDEL()
        {
            _DEL++;
        }

        public void AddGINS()
        {
            _GINS++;
        }

        public void AddGDEL()
        {
            _GDEL++;
        }
        #endregion

        public void Save(Result result)
        { 
            _sessionStarted = false;
            if (result != null) {
                _Error = String.Format("{0} | {1}", _Error, result.Message);                
                _status = 2;
            }

            SaveLog();
        }
        #endregion

        #region Private
        private void Init()
        {
            _ldapPraviloID = 0;
            _ldapStreznikID = 0;
            _INS = 0;
            _DEL = 0;
            _UPD = 0;
            _GINS = 0;
            _GDEL = 0;
            _status = 0;
            _startTime = DateTime.Now;
            _Report = ""; _Error = ""; _Warning = "";
        }

        private void SaveLog()
        {             
            LdapLog ldapLog = EntityFactory.CreateInstance<LdapLog>();
            TimeSpan ts = DateTime.Now - _startTime;

            if (_GINS > 0) _Report = String.Format("{0} | Uporabniki dodani v skupine: {1}", _Report, _GINS);
            if (_GDEL > 0) _Report = String.Format("{0} | Uporabniki izbrisani iz skupin: {1}", _Report, _GDEL);

            ldapLog.LdapPraviloID = _ldapPraviloID;
            ldapLog.LdapStreznikID = _ldapStreznikID;
            ldapLog.TipSinhronizacije = (int)_syncType;
            ldapLog.Datum = DateTime.Now;
            ldapLog.Status = _status;
            ldapLog.CountIns = _INS;
            ldapLog.CountDel = _DEL;
            ldapLog.CountUpd = _UPD;
            ldapLog.Porocilo = _Report;
            ldapLog.Opozorilo = _Warning;
            ldapLog.Napaka = _Error;
            ldapLog.PorabljenCas = (int)ts.TotalSeconds;

            var slLdapLog = ServiceFactory.GetService<ILdapLogiService>();
            slLdapLog.Update(ldapLog, _session);
        }
        #endregion

    }
}
